package com.example.prison.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.entity.PersonnelManagement;
import com.example.prison.service.PersonnelManagementService;
import com.example.prison.mapper.PersonnelManagementMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author jiang
* @description 针对表【personnel_management】的数据库操作Service实现
* @createDate 2023-05-26 16:07:49
*/
@Service
public class PersonnelManagementServiceImpl extends ServiceImpl<PersonnelManagementMapper, PersonnelManagement>
    implements PersonnelManagementService{


    @Autowired
    private PersonnelManagementMapper personnelManagementMapper;

    @Override
    public PageInfo<PersonnelManagement> get(int pageNumber, int pageSize,String personnelName) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(personnelManagementMapper.get(personnelName));
    }

    @Override
    public int add(PersonnelManagement personnel) {
//        String name = personnel.getName();
        String phone = personnel.getPhone();
        if (ObjectUtils.isNotEmpty(personnelManagementMapper.getOneByPhone(phone))) {
            return -1;
        } else {
            personnel.setIsPushPeople(1);
            personnelManagementMapper.add(personnel);
            return 1;
        }
    }

    @Override
    public void delete(Integer[] ids) {
        personnelManagementMapper.deletePersonnel(ids);
    }

    @Override
    public int update(PersonnelManagement personnel, Integer id) {
        PersonnelManagement personnelDTO = personnelManagementMapper.getById(id);
        personnelManagementMapper.updatePersonnel(personnel, id);
        List<PersonnelManagement> personnelList = personnelManagementMapper.getBySameName(personnel.getName());
        if(personnelList.size() > 1) {
            personnelManagementMapper.updatePersonnelFail(personnelDTO, id);
            return -1;
        } else {
            return 1;
        }
    }

    @Override
    public PersonnelManagement getById(Integer id) {
        return personnelManagementMapper.getById(id);
    }


}




